<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="default" :inline="true" label-width="100px">
      <el-form-item label="鉴定人姓名" prop="identifierName">
        <el-input
          v-model="queryParams.identifierName"
          placeholder="请输入鉴定人姓名"
          clearable
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="contactInfo">
        <el-input
          v-model="queryParams.contactInfo"
          placeholder="请输入联系电话"
          clearable
        />
      </el-form-item>
      <el-form-item label="入职日期" prop="entryDate">
        <el-date-picker clearable
                        v-model="queryParams.entryDate"
                        type="date"
                        format="YYYY-MM-DD HH:mm:ss"
                        value-format="YYYY-MM-DD HH:mm:ss"
                        placeholder="请选择入职日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-perms="['archives:identifier:add']"
          v-preventReClick
        >新增
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="identifierList">
      <el-table-column label="主键" align="center" prop="identifierId" />
      <el-table-column label="鉴定人姓名" align="center" prop="identifierName" />
      <el-table-column label="用户性别" align="center" prop="identifierGender">
        <template #default="scope">
          {{sexOptions[scope.row.identifierGender].txt}}
        </template>
      </el-table-column>
      <el-table-column label="出生日期" align="center" prop="identifierBirthdate" width="180" />
      <el-table-column label="资格证书" align="center" prop="identifierQualification" />
      <el-table-column label="专业领域" align="center" prop="identifierSpecialty" />
      <el-table-column label="联系电话" align="center" prop="contactInfo" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <status-tag :status="scope.row.status" :status-enum="statusOptions"></status-tag>
        </template>
      </el-table-column>
      <el-table-column label="入职日期" align="center" prop="entryDate" width="180" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            type="primary"
            link
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-perms="['archives:identifier:edit']"
            v-preventReClick
          >修改
          </el-button>
          <el-button
            type="primary"
            link
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-perms="['archives:identifier:remove']"
            v-preventReClick
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-if="total>0"
      v-model:current-page="queryParams.pageNum"
      :page-size="10"
      layout="total, prev, pager, next"
      :total="total"
      style="float: right"
    />

    <!-- 添加或修改鉴定人对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="鉴定人姓名" prop="identifierName">
          <el-input v-model="form.identifierName" placeholder="请输入鉴定人姓名" />
        </el-form-item>
        <el-form-item label="出生日期" prop="identifierBirthdate">
          <el-date-picker clearable
                          v-model="form.identifierBirthdate"
                          type="date"
                          format="YYYY-MM-DD HH:mm:ss"
                          value-format="YYYY-MM-DD HH:mm:ss"
                          placeholder="请选择出生日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="用户性别">
          <el-select v-model="form.identifierGender" placeholder="请选择性别">
            <el-option
              v-for="item in convertToOptions(sexOptions)"
              :key="item.value"
              :label="item.label"
              :value="item.value.toString()"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio v-for="item in convertToOptions(statusOptions)" :key="item.value" size="large"
                      :value="item.value.toString()">{{ item.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="资格证书" prop="identifierQualification">
          <el-input v-model="form.identifierQualification" placeholder="请输入资格证书" />
        </el-form-item>
        <el-form-item label="专业领域" prop="identifierSpecialty">
          <el-input v-model="form.identifierSpecialty" placeholder="请输入专业领域" />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactInfo">
          <el-input v-model="form.contactInfo" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="入职日期" prop="entryDate">
          <el-date-picker clearable
                          v-model="form.entryDate"
                          type="date"
                          format="YYYY-MM-DD HH:mm:ss"
                          value-format="YYYY-MM-DD HH:mm:ss"
                          placeholder="请选择入职日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import StatusTag from '@/components/StatusTag.vue'
import { convertToOptions } from '@/utils/util.js'
import { IdentifierService } from '@/api/archives/identifier.js'

export default {
  name: 'IdentifierIndex',
  components: { StatusTag },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 鉴定人表格数据
      identifierList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        identifierName: null,
        contactInfo: null,
        status: null,
        entryDate: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        identifierName: [
          { required: true, message: '鉴定人姓名不能为空', trigger: 'blur' }
        ],
        identifierQualification: [
          { required: true, message: '资格证书不能为空', trigger: 'blur' }
        ],
        identifierSpecialty: [
          { required: true, message: '专业领域不能为空', trigger: 'blur' }
        ],
        contactInfo: [
          { required: true, message: '联系电话不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '状态不能为空', trigger: 'change' }
        ],
        entryDate: [
          { required: true, message: '入职日期不能为空', trigger: 'blur' }
        ]
      },
      statusOptions: {
        '0': {
          type: 'primary',
          txt: '在职'
        },
        '1': {
          type: 'danger',
          txt: '离职'
        }
      },
      sexOptions: {
        '0': {
          type: 'primary',
          txt: '男'
        },
        '1': {
          type: 'primary',
          txt: '女'
        },
        '2': {
          type: 'primary',
          txt: '未知'
        }
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    convertToOptions,
    /** 查询鉴定人列表 */
    getList() {
      this.loading = true
      IdentifierService.listIdentifier(this.queryParams).then(response => {
        this.identifierList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        identifierId: null,
        identifierName: null,
        identifierGender: null,
        identifierBirthdate: null,
        identifierQualification: null,
        identifierSpecialty: null,
        contactInfo: null,
        status: '0',
        entryDate: null,
        createTime: null,
        updateTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs.queryForm.resetFields()
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true
      this.reset()
      this.title = '添加鉴定人'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const identifierId = row.identifierId || this.ids
      IdentifierService.getIdentifier(identifierId).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改鉴定人'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.identifierId != null) {
            IdentifierService.updateIdentifier(this.form).then(response => {
              this.$message.success('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            IdentifierService.addIdentifier(this.form).then(response => {
              this.$message.success('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const identifierIds = row.identifierId || this.ids
      this.$confirm('是否确认删除鉴定人编号为"' + identifierIds + '"的数据项？').then(function() {
        return IdentifierService.delIdentifier(identifierIds)
      }).then(() => {
        this.getList()
        this.$message.success('删除成功')
      }).catch(() => {
      })
    }
  }
}
</script>
